home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / anlogclk / anlogclk.sx next >
Encoding:
Text File  |  1996-05-21  |  9.6 KB  |  269 lines  |  [TEXT/ttxt]

  1. in module AnalogClockModule
  2.  
  3. -- Note: 0 and (2 * pi) degrees radian is at 3 o'clock and increases clockwise
  4. global constant angleAt12 := (1.5 * pi)
  5. global constant anglePerSec := (pi / 30)
  6.  
  7.  
  8. class ClockHands (RotatingShape)
  9. end
  10.  
  11. --*******************************************************************************
  12. --*        Class name:    AnalogClock
  13. --*                                            
  14. --*     Inherits from: TwoDMultiPresenter                                    
  15. --*        Class type: Concrete
  16. --*         Component: Spaces and Presenters
  17. --*
  18. --*       Description: This is a subclass of TwoDMultiPresenter which consists of
  19. --*                    3 clock hands: the hour hand, the minute hand and the second
  20. --*                    hand.  To start the clock, call the startClock method. It 
  21. --*                    sets the time then starts a callback which updates the clock 
  22. --*                    every second. Only the second hand gets updated every second. 
  23. --*                    The minute and hour hands get updated on the minute.
  24. --*
  25. --*             Usage: ac := new AnalogClock radius:60 \
  26. --*                                          clockFace:<some bitmap> \
  27. --*                                          hourHand:<some bitmap> \
  28. --*                                          minHand:<some bitmap> \
  29. --*                                          hourAxis:(new Point x:0 y:0) \
  30. --*                                          minAxis:(new Point x:0 y:0)
  31. --*
  32. --*               IVs:    radius
  33. --*                    hourHand
  34. --*                    minuteHand
  35. --*                    secondHand            
  36. --*
  37. --*           Methods:    makeClockHand
  38. --*                    setHours
  39. --*                    setMinutes
  40. --*                    setSeconds
  41. --*                    setTime
  42. --*                    updateClock
  43. --*                    startClock
  44. --*                    init
  45. --*                    afterInit
  46. --*                    
  47. --*    Required files:    rot8shp.sx
  48. --*                    
  49. --*             Notes: 
  50. --*
  51. --*            Author:    Su Quek - Kaleida Labs, Inc.
  52. --*******************************************************************************
  53. class AnalogClock (TwoDMultiPresenter)
  54. inst vars
  55.     radius
  56.     hourHand
  57.     minuteHand
  58.     secondHand
  59. end
  60.  
  61. --*=============================================================================*
  62. --*       Method name:    makeClockHand
  63. --*             Class:    AnalogClock
  64. --*             Usage: makeClockHand self target
  65. --*                        target    - Bitmap 
  66. --*-----------------------------------------------------------------------------*
  67. --*       Description: Creates a ClockHand. 
  68. --*=============================================================================*
  69. method makeClockHand self {class AnalogClock} target axis ->
  70. (
  71.     local clockRadius := self.radius
  72.     local theHand := new ClockHands target:target axisPos:axis
  73.     theHand.fill := blackBrush
  74.     theHand.position := new Point x:(clockRadius - axis.x) y:(clockRadius - axis.y)
  75.     prepend self theHand
  76.         
  77.     return theHand
  78. )
  79.  
  80. --*=============================================================================*
  81. --*       Method name:    makeSecondHand
  82. --*             Class:    AnalogClock
  83. --*             Usage: makeSecondHand self
  84. --*-----------------------------------------------------------------------------*
  85. --*       Description: Creates a Line. 
  86. --*=============================================================================*
  87. method makeSecondHand self {class AnalogClock} ->
  88. (
  89.     local clockRadius := self.radius
  90.     local theLine := (new Line x2:0 y2:(0.8 * clockRadius) )
  91.     local secondHand := new TwoDShape boundary:theLine \
  92.                                       stroke:(new Brush color:redColor)
  93.     secondHand.position := new Point x:clockRadius y:clockRadius
  94.     prepend self secondHand
  95.         
  96.     self.secondHand := secondHand.boundary
  97.  
  98.     return secondHand.boundary
  99. )
  100.  
  101. --*=============================================================================*
  102. --*       Method name:    setHours
  103. --*             Class:    AnalogClock
  104. --*             Usage: setHours self
  105. --*-----------------------------------------------------------------------------*
  106. --*       Description: Rotates the hour hand clockwise by 0.5 degrees.
  107. --*                    # degrees/minute = (30 degrees) / (60 minutes) = 0.5
  108. --*=============================================================================*
  109. method setHours self {class AnalogClock} ->
  110. (
  111.     rotate self.hourHand 0.5 @degrees
  112. )
  113.  
  114. --*=============================================================================*
  115. --*       Method name:    setMinutes
  116. --*             Class:    AnalogClock
  117. --*             Usage: setMinutes self
  118. --*-----------------------------------------------------------------------------*
  119. --*       Description: Rotates the minute hand clockwise by 6 degrees.
  120. --*                    # degrees/minute = (360 degrees) / (60 minutes) = 6
  121. --*=============================================================================*
  122. method setMinutes self {class AnalogClock} ->
  123. (
  124.     rotate self.minuteHand 6 @degrees
  125. )
  126.  
  127. --*=============================================================================*
  128. --*       Method name:    setSeconds
  129. --*             Class:    AnalogClock
  130. --*             Usage: setSeconds self
  131. --*-----------------------------------------------------------------------------*
  132. --*       Description: Rotates the second hand by the angle moved per second.
  133. --*                    (angle @ 12 o'clock) + ((# second) * (2 * pi) / (60 seconds)) 
  134. --*
  135. --*                    angleAt12 = (1.5 * pi); anglePerSec = (pi / 30)
  136. --*=============================================================================*
  137. method setSeconds self {class AnalogClock} ->
  138. (
  139.     local theSec := theCalendarClock.date.seconds    
  140.     self.secondHand.angle := angleAt12 + (theSec * anglePerSec)
  141.     notifyChanged self true
  142.     
  143.     return theSec
  144. )
  145.  
  146. --*=============================================================================*
  147. --*       Method name:    setTime
  148. --*             Class:    AnalogClock
  149. --*             Usage: setTime self
  150. --*-----------------------------------------------------------------------------*
  151. --*       Description: Sets the hour, minute and second hands of the clock.
  152. --*                    Hour: (# hour) * (360 degrees) / (12 hours) 
  153. --*                          (# minute) * (30 degrees) / (60 minutes)
  154. --*                    Minute: (# minute) * (360 degrees) / (60 minutes) 
  155. --*                    Second:    (angle @ 12 o'clock) + 
  156. --*                            ((# second) * (2 * pi) / (60 seconds))
  157. --*
  158. --*                    angleAt12 = (1.5 * pi); anglePerSec = (pi / 30)
  159. --*=============================================================================*
  160. method setTime self {class AnalogClock}  ->
  161. (
  162.     local theDate := theCalendarClock.date
  163.     local theHour := theDate.hours
  164.     local theMin  := theDate.minutes
  165.     local theSec  := theDate.seconds
  166.  
  167.     --*=========================================================================*
  168.     --* Set the second hand
  169.     --*=========================================================================*
  170.     self.secondHand.angle := angleAt12 + (theSec * anglePerSec)
  171.  
  172.     --*=========================================================================*
  173.     --* Set the minute hand
  174.     --*=========================================================================*
  175.     rotate self.minuteHand (theMin * 360 / 60) @degrees            
  176.  
  177.     --*=========================================================================*
  178.     --* Set the hour hand
  179.     --*=========================================================================*
  180.     rotate self.hourHand (theHour * 360 / 12) @degrees        
  181.     rotate self.hourHand (theMin * 30 / 60) @degrees        
  182.     
  183.     return 
  184. )
  185.  
  186. --*=============================================================================*
  187. --*       Method name:    updateClock
  188. --*             Class:    AnalogClock
  189. --*             Usage: updateClock self 
  190. --*-----------------------------------------------------------------------------*
  191. --*       Description: Updates the hour, minute and second hands of the clock. 
  192. --*                    The minute and hour hands get updated on the minute.
  193. --*=============================================================================*
  194. method updateClock self {class AnalogClock} ->
  195. (
  196.     if ((setSeconds self) = 0) do
  197.     (
  198.         setMinutes self
  199.         setHours self
  200.     )
  201.  
  202.     return
  203. )
  204.  
  205. --*=============================================================================*
  206. --*       Method name:    startClock
  207. --*             Class:    AnalogClock
  208. --*             Usage: startClock self 
  209. --*-----------------------------------------------------------------------------*
  210. --*       Description: Sets the time then starts a callback which updates the clock
  211. --*                    every second.
  212. --*=============================================================================*
  213. method startClock self {class AnalogClock} ->
  214. (
  215.     setTime self 
  216.     addPeriodicCallback theCalendarClock updateClock self #() 1
  217. )
  218.     
  219.  
  220. --*=============================================================================*
  221. --*       Method name:    init
  222. --*             Class:    AnalogClock
  223. --*             Usage: init self [radius:<Number>] \
  224. --*                              [clockFace:<Bitmap>] \
  225. --*                              [hourHand:<Bitmap>] \
  226. --*                              [hourAxis:<Point>] \
  227. --*                              [minHand:<Bitmap>] \
  228. --*                              [minAxis:<Point>] 
  229. --*-----------------------------------------------------------------------------*
  230. --*       Description: Creates and initializes the TwoDMultiPresenter.
  231. --*=============================================================================*
  232. method init self {class AnalogClock} #rest args \
  233.                                      #key radius:(75) \
  234.                                           clockFace:(new Bitmap) \
  235.                                           hourHand:(new Bitmap) \
  236.                                           hourAxis:(new Point x:0 y:0) \
  237.                                           minHand:(new Bitmap) \
  238.                                           minAxis:(new Point x:0 y:0) ->
  239. (
  240.     apply nextmethod self boundary:clockFace.bbox args
  241.     return self
  242. )
  243.  
  244. --*=============================================================================*
  245. --*       Method name:    afterInit
  246. --*             Class:    AnalogClock
  247. --*             Usage: afterInit self [radius:<Number>]
  248. --*-----------------------------------------------------------------------------*
  249. --*       Description: Sets the radius IV and creates the hands of the clock.
  250. --*=============================================================================*
  251. method afterinit self {class AnalogClock} #rest args \
  252.                                           #key radius:(75) \
  253.                                           clockFace:(new Bitmap) \
  254.                                           hourHand:(new Bitmap) \
  255.                                           hourAxis:(new Point x:0 y:0) \
  256.                                           minHand:(new Bitmap) \
  257.                                           minAxis:(new Point x:0 y:0) ->
  258. (
  259.     self.radius := radius
  260.     
  261.     append self (new TwoDShape boundary:clockFace fill:defaultBrush)
  262.     self.hourHand := (makeClockHand self hourHand hourAxis)
  263.     self.minuteHand := (makeClockHand self minHand minAxis)
  264.     self.secondHand := (makeSecondHand self)
  265.     
  266.     return self
  267. )
  268. "Loaded anlogclk.sx"
  269.